import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:nft_shop/my/conteroller/my_write_conteroller.dart';
import 'package:nft_shop/my/page/write/my_album_page.dart';
import 'package:nft_shop/my/page/write/my_works_creation_page.dart';
import 'package:nft_shop/my/page/write/review_page.dart';
import 'package:nft_shop/net/service_repository.dart';
import 'package:nft_shop/res/constant.dart';
import 'package:nft_shop/res/global.dart';
import 'package:nft_shop/sift/page/sift_page.dart';
import 'package:nft_shop/widgets/custom_underline_tabIndicator.dart';
import 'package:nft_shop/widgets/load_image.dart';
import 'package:nft_shop/widgets/my_app_bar.dart';
import 'package:nft_shop/widgets/my_scroll_view.dart';
import 'package:nft_shop/widgets/status_view.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'my_write_list_page.dart';

class MyWritePage extends StatefulWidget {
  const MyWritePage({Key? key}) : super(key: key);

  @override
  _MyWritePageState createState() => _MyWritePageState();
}

class _MyWritePageState extends State<MyWritePage>
    with SingleTickerProviderStateMixin {
  bool isCreation = false;

  ///状态
  int status = LoadStatusType.loading;

  MyWriteConteroller conteroller = MyWriteConteroller.to;

  TabController? _tabController;
  final PageController _pageController = PageController(initialPage: 0);

  ///刷新组件
  RefreshController refreshController =
      RefreshController(initialRefresh: false);

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _tabController = TabController(vsync: this, length: 2);
    // loadData();
  }

  //获取数据
  loadData() async {
    List list = [];
    try {
      list = await ServiceRepository.specialAlbumList(
          page: 1, pageSize: 20, status: 2);
      if (list.length != 0) {
        isCreation = true;
      } else {
        isCreation = false;
      }
      status = LoadStatusType.success;
      setState(() {});
    } catch (s, e) {
      isCreation = false;
      status = LoadStatusType.empty;
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    print("创作服务刷新");
    return Scaffold(
        appBar: MyAppBar(
          centerTitle: "创作服务",
          actionName: "审核列表",
          onPressed: () => Get.to(ReviewPage()),
        ),
        body: GetBuilder<MyWriteConteroller>(
            id: MyWriteConteroller.MyWriteConterollerID,
            builder: (logic) {
              return SmartRefresher(
                controller: refreshController,
                header: WaterDropHeader(),
                footer: ClassicFooter(
                  noDataText: "",
                ),
                onRefresh: () {},
                enablePullUp: true,
                child: NestedScrollView(
                  physics: const ClampingScrollPhysics(),
                  headerSliverBuilder: (context, innerBoxIsScrolled) =>
                      _sliverBuilder(context),
                  body: PageView.builder(
                    itemCount: 2,
                    onPageChanged: _onPageChange,
                    controller: _pageController,
                    itemBuilder: (_, index) =>
                        MyWriteListPage(index: index + 1),
                  ),
                ),
              );
            }));
  }

  ///置顶
  List<Widget> _sliverBuilder(BuildContext context) {
    return <Widget>[
      //头部
      SliverToBoxAdapter(
        child: buildTopWidget(),
      ),
      //选项卡
      SliverPersistentHeader(
        pinned: true,
        floating: true,
        delegate: SliverAppBarDelegate(
          DecoratedBox(
            decoration: BoxDecoration(color: Global.kTheme.colorBackground1),
            child: tabsWidget(),
          ),
          88.0,
        ),
      ),
    ];
  }

  //创建作品
  Widget buildTopWidget() {
    Color color1;
    Color color2;
    if (isCreation) {
      color1 = Color(0xFF89F1D9);
      color2 = Color(0xFF67E6CA);
    } else {
      color1 = Color(0xFFDCDCDC);
      color2 = Color(0xFFDCDCDC);
    }
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 17, vertical: 13),
      decoration: BoxDecoration(
          color: Global.kTheme.colorBackground1,
          border: Border(
            top: Divider.createBorderSide(context,
                color: Global.kTheme.colorLine, width: 1),
            // bottom: Divider.createBorderSide(context,
            //     color: Color(0xFFF7F7FA), width: 8),
          )),
      child: Row(
        children: [
          Expanded(
            flex: 1,
            child: InkWell(
              onTap: () => Get.to(MyAlbumPage()),
              child: Container(
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(4),
                  gradient: LinearGradient(
                    begin: Alignment.topCenter,
                    end: Alignment.bottomCenter,
                    colors: [
                      Color(0xFF474747),
                      Color(0xFF222222),
                    ],
                  ),
                ),
                height: 72,
                child: Center(
                  child: Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      LoadAssetImage(
                        "my_write_item1",
                        width: 20,
                        height: 20,
                      ),
                      SizedBox(
                        width: 8,
                      ),
                      Text(
                        "专辑专区",
                        style: TextStyle(
                            fontSize: 14,
                            color: Global.kTheme.textPrimary5,
                            fontFamily: Global.mySemiboldFont),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),
          SizedBox(
            width: 6,
          ),
          Expanded(
            flex: 1,
            child: InkWell(
              onTap: () {
                Get.to(MyWorksCreationPage());
              },
              child: Container(
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(4),
                  gradient: LinearGradient(
                    begin: Alignment.topCenter,
                    end: Alignment.bottomCenter,
                    colors: [
                      color1,
                      color2,
                    ],
                  ),
                ),
                height: 72,
                child: Center(
                  child: Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      LoadAssetImage(
                        "my_write_item2",
                        width: 20,
                        height: 20,
                      ),
                      SizedBox(
                        width: 8,
                      ),
                      Text(
                        "创建作品",
                        style: TextStyle(
                            fontSize: 14,
                            color: Global.kTheme.textPrimary5,
                            fontFamily: Global.mySemiboldFont),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  ///选项卡
  Widget tabsWidget() {
    List<String> titleList = [];
    titleList.add("已铸造(${conteroller.zhuzaoy})");
    titleList.add("未铸造(${conteroller.zhuzaon})");

    return Container(
      color: Global.kTheme.colorBackground1,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: const EdgeInsets.only(top: 18, left: 15, right: 15),
            child: Text(
              "我的NFT",
              style: TextStyle(
                  fontSize: 16,
                  color: Global.kTheme.textPrimary1,
                  fontFamily: Global.mySemiboldFont),
            ),
          ),
          TabBar(
            isScrollable: true,
            controller: _tabController,
            labelStyle: TextStyle(
              fontSize: 16,
              fontFamily: Global.mySemiboldFont,
            ),
            unselectedLabelStyle: TextStyle(
              fontSize: 14,
              fontFamily: Global.mySemiboldFont,
            ),
            indicatorSize: TabBarIndicatorSize.label,
            unselectedLabelColor: Global.kTheme.textPrimary3,
            labelColor: Global.kTheme.textPrimary4,
            indicatorPadding:
            EdgeInsets.only(bottom: 5, left: 22, right: 22),
            indicator: CustomUnderlineTabIndicator(
                borderSide: BorderSide(
                  color: Global.kTheme.textPrimary4,
                  width: 3,
                )),
            tabs: List.generate(
                titleList.length,
                    (index) => Tab(
                  text: titleList[index],
                )),
            onTap: (index) {
              if (!mounted) {
                return;
              }
              _pageController.jumpToPage(index);
            },
          ),
        ],
      ),
    );
  }

  void _onPageChange(int index) {
    _tabController?.animateTo(index);
    conteroller.sectionIndex = index;
  }
}
